<!-- 邀请码和绑定手机页面（一起），绑定手机跳转到绑定手机页面 -->
<template>
  <div id="inviterAndPhone">
    <c-title :hide="false" text="绑定信息"></c-title>
    <van-cell-group inset style="margin-top: 12px;">
      <van-field v-model="inviter" :required="requiredInviter" label="邀请码" placeholder="请输入邀请码" input-align="right" />
      <!-- <van-field label="手机号" :value="phone ? phone : '去绑定'" readonly input-align="right" is-link @click="toBindPhone" /> -->
    </van-cell-group>
    <van-button type="danger" round @click="beforeBind('invite')" style="margin-top: 18px; width: 80%;">确认绑定</van-button>
    <!-- <van-button type="default" round @click="beforeBind('home')" style="margin-top: 18px; width: 80%;">跳过</van-button> -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      requiredInviter: false,
      inviter: "", //邀请码
      phone: "" //已绑定的手机号
    };
  },
  mounted() {
    this.requiredInviter = this.$route.query.requiredInviter; //是否开启了邀请码页面，有则强制绑定
    // this.getUserinfo();
  },
  methods: {
    async getUserinfo() {
      let { result, msg, data } = await $http.get("member.member.getUserInfo", {}, ".");
      if (result == 0) return this.$toast(msg);
      this.phone = data.mobile;
    },
    toBindPhone() {
      if (!this.phone) this.$router.push(this.fun.getUrl("editmobile", {}));
    },
    beforeBind(type){
      let _this = this;
      jsBridge.getCurrentBTYAddress({},(v)=>{
        if(v){
          if(type == 'invite') return _this.bindInfo(v);
          if(type == 'home') return _this.toHome(v);
        }
      });
    },
    async bindInfo(wallet) {
      if (!this.inviter) return this.$toast("请填写邀请码！");
      let { result, msg, data } = await $http.get("plugin.lsp-wallet-middleground.frontend.invite.bind", { invite_code: this.inviter,wallet_site: wallet }, ".");
      if (result == 0) return this.$toast(msg);
      window.yzFirstlogin = 1;
      sessionStorage.setItem("isset", 1);
      this.$store.commit("setInviter", 1);
      this.toHome();
    },
    async toHome(wallet) {
      if(!wallet) return this.$router.replace(this.fun.getUrl("home"));
      let { result, msg } = await $http.get("plugin.lsp-wallet-middleground.frontend.invite.skip", {wallet_site: wallet}, ".");
      if (result == 0) return this.$toast(msg);
      this.$router.replace(this.fun.getUrl("home"));
    }
  }
};
</script>